home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
kernel
/
proc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-23
|
19KB
|
587 lines
/* This file contains essentially all of the process and message handling.
* It has two main entry points from the outside:
*
* sys_call: called when a process or task does SEND, RECEIVE or SENDREC
* interrupt: called by interrupt routines to send a message to task
*
* It also has several minor entry points:
*
* lock_ready: put a process on one of the ready queues so it can be run
* lock_unready: remove a process from the ready queues
* lock_sched: a process has run too long; schedule another one
* lock_mini_send: send a message (used by interrupt signals, etc.)
* lock_pick_proc: pick a process to run (used by system initialization)
* unhold: repeat all held-up interrupts
*/
#include "kernel.h"
#include <minix/callnr.h>
#include <minix/com.h>
#include "proc.h"
PRIVATE unsigned char switching; /* nonzero to inhibit interrupt() */
FORWARD int mini_send();
FORWARD int mini_rec();
FORWARD void pick_proc();
FORWARD void ready();
FORWARD void sched();
FORWARD void unready();
#if (CHIP == INTEL)
#define CopyMess(s,sp,sm,dp,dm) \
cp_mess(s,(sp)->p_map[D].mem_phys,sm,(dp)->p_map[D].mem_phys,dm)
#endif
#if (CHIP == M68000)
#define CopyMess(s,sp,sm,dp,dm) \
cp_mess(s,sp,sm,dp,dm)
#endif
/*===========================================================================*
* interrupt *
*===========================================================================*/
PUBLIC void interrupt(task)
int task; /* number of task to be started */
{
/* An interrupt has occurred. Schedule the task that handles it. */
register struct proc *rp; /* pointer to task's proc entry */
rp = proc_addr(task);
/* If this call would compete with other process-switching functions, put
* it on the 'held' queue to be flushed at the next non-competing restart().
* The competing conditions are:
* (1) k_reenter == (typeof k_reenter) -1:
* Call from the task level, typically from an output interrupt
* routine. An interrupt handler might reenter interrupt(). Rare,
* so not worth special treatment.
* (2) k_reenter > 0:
* Call from a nested interrupt handler. A previous interrupt handler
* might be inside interrupt() or sys_call().
* (3) switching != 0:
* Some process-switching function other than interrupt() is being
* called from the task level, typically sched() from CLOCK. An
* interrupt handler might call interrupt and pass the k_reenter test.
*/
if (k_reenter != 0 || switching) {
lock();
if (!rp->p_int_held) {
rp->p_int_held = TRUE;
if (held_head != NIL_PROC)
held_tail->p_nextheld = rp;
else
held_head = rp;
held_tail = rp;
rp->p_nextheld = NIL_PROC;
}
unlock();
return;
}
/* If task is not waiting for an interrupt, record the blockage. */
if ( (rp->p_flags & (RECEIVING | SENDING)) != RECEIVING ||
!isrxhardware(rp->p_getfrom)) {
rp->p_int_blocked = TRUE;
return;
}
/* Destination is waiting for an interrupt.
* Send it a message with source HARDWARE and type HARD_INT.
* No more information can be reliably provided since interrupt messages
* are not queued.
*/
rp->p_messbuf->m_source = HARDWARE;
rp->p_messbuf->m_type = HARD_INT;
rp->p_flags &= ~RECEIVING;
rp->p_int_blocked = FALSE;
/* Make rp ready and run it unless a task is already running. This is
* ready(rp) in-line for speed.
*/
if (rdy_head[TASK_Q] != NIL_PROC)
rdy_tail[TASK_Q]->p_nextready = rp;
else
#if (CHIP != M68000)
proc_ptr =
#endif
rdy_head[TASK_Q] = rp;
rdy_tail[TASK_Q] = rp;
rp->p_nextready = NIL_PROC;
}
/*===========================================================================*
* sys_call *
*===========================================================================*/
PUBLIC int sys_call(function, src_dest, m_ptr)
int function; /* SEND, RECEIVE, or BOTH */
int src_dest; /* source to receive from or dest to send to */
message *m_ptr; /* pointer to message */
{
/* The only system calls that exist in MINIX are sending and receiving
* messages. These are done by trapping to the kernel with an INT instruction.
* The trap is caught and sys_call() is called to send or receive a message
* (or both). The caller is always given by proc_ptr.
*/
register struct proc *rp;
int n;
/* Check for bad system call parameters. */
if (!isoksrc_dest(src_dest)) return(E_BAD_SRC);
rp = proc_ptr;
if (function != BOTH && isuserp(rp))
return(E_NO_PERM); /* users only do BOTH */
/* The parameters are ok. Do the call. */
if (function & SEND) {
/* Function = SEND or BOTH. */
n = mini_send(rp, src_dest, m_ptr);
if (function == SEND || n != OK)
return(n); /* done, or SEND failed */
}
/* Function = RECEIVE or BOTH.
* We have checked user calls are BOTH, and trust 'function' otherwise.
*/
return(mini_rec(rp, src_dest, m_ptr));
}
/*===========================================================================*
* mini_send *
*===========================================================================*/
PRIVATE int mini_send(caller_ptr, dest, m_ptr)
register struct proc *caller_ptr; /* who is trying to send a message? */
int dest; /* to whom is message being sent? */
message *m_ptr; /* pointer to message buffer */
{
/* Send a message from 'caller_ptr' to 'dest'. If 'dest' is blocked waiting
* for this message, copy the message to it and unblock 'dest'. If 'dest' is
* not waiting at all, or is waiting for another source, queue 'caller_ptr'.
*/
register struct proc *dest_ptr, *next_ptr;
vir_bytes vb; /* message buffer pointer as vir_bytes */
vir_clicks vlo, vhi; /* virtual clicks containing message to send */
/* User processes are only allowed to send to FS and MM. Check for this. */
if (isuserp(caller_ptr) && !isservn(dest)) return(E_BAD_DEST);
dest_ptr = proc_addr(dest); /* pointer to destination's proc entry */
if (dest_ptr->p_flags & P_SLOT_FREE) return(E_BAD_DEST); /* dead dest */
/* Check for messages wrapping around top of memory or outside data seg. */
vb = (vir_bytes) m_ptr;
vlo = vb >> CLICK_SHIFT; /* vir click for bottom of message */
vhi = (vb + MESS_SIZE - 1) >> CLICK_SHIFT; /* vir click for top of msg */
if (vhi < vlo ||
vhi - caller_ptr->p_map[D].mem_vir >= caller_ptr->p_map[D].mem_len)
return(E_BAD_ADDR);
/* Check for deadlock by 'caller_ptr' and 'dest' sending to each other. */
if (dest_ptr->p_flags & SENDING) {
next_ptr = caller_ptr->p_callerq;
while (next_ptr != NIL_PROC) {
if (next_ptr == dest_ptr) return(ELOCKED);
next_ptr = next_ptr->p_sendlink;
}
}
/* Check to see if 'dest' is blocked waiting for this message. */
if ( (dest_ptr->p_flags & (RECEIVING | SENDING)) == RECEIVING &&
(dest_ptr->p_getfrom == ANY ||
dest_ptr->p_getfrom == proc_number(caller_ptr))) {
/* Destination is indeed waiting for this message. */
CopyMess(proc_number(caller_ptr), caller_ptr, m_ptr, dest_ptr,
dest_ptr->p_messbuf);
dest_ptr->p_flags &= ~RECEIVING; /* deblock destination */
if (dest_ptr->p_flags == 0) ready(dest_ptr);
} else {
/* Destination is not waiting. Block and queue caller. */
caller_ptr->p_messbuf = m_ptr;
if (caller_ptr->p_flags == 0) unready(caller_ptr);
caller_ptr->p_flags |= SENDING;
/* Process is now blocked. Put in on the destination's queue. */
if ( (next_ptr = dest_ptr->p_callerq) == NIL_PROC)
dest_ptr->p_callerq = caller_ptr;
else {
while (next_ptr->p_sendlink != NIL_PROC)
next_ptr = next_ptr->p_sendlink;
next_ptr->p_sendlink = caller_ptr;
}
caller_ptr->p_sendlink = NIL_PROC;
}
return(OK);
}
/*===========================================================================*
* mini_rec *
*===========================================================================*/
PRIVATE int mini_rec(caller_ptr, src, m_ptr)
register struct proc *caller_ptr; /* process trying to get message */
int src; /* which message source is wanted (or ANY) */
message *m_ptr; /* pointer to message buffer */
{
/* A process or task wants to get a message. If one is already queued,
* acquire it and deblock the sender. If